home *** CD-ROM | disk | FTP | other *** search
/ PC Format (UK) 188 / 01-04 PC Format 188 [2006-06] DVD side 1_.iso / DiscContents / Workshops / Workshops_Games / Sphere / InstallSphere1.0.exe / games / fg-demo / scripts / Battle.js < prev    next >
Text File  |  2003-02-18  |  11KB  |  490 lines

  1. function StartBattle(battle, background, canlose)
  2. {
  3.     battle.reset();
  4.     if (battle.boss_battle)
  5.     {
  6.         ;// Play boss music
  7.     }
  8.     if (!battle.boss_battle)
  9.     {
  10.         //Battles.normal_music.play(true);
  11.     }
  12.     for (var i = 0; i < Game.psize; i ++)
  13.     {
  14.         Game.party[i].time = 0;
  15.         Game.party[i].x = 248;
  16.         Game.party[i].y = i * 40 + 70;
  17.     }
  18.     RenderMap();
  19.     Swish(2, 4000);
  20.     battle.background = background;
  21.     Game.battleopen = true;
  22.     Game.curBattle = battle;
  23.     battle.draw();
  24.     FadeIn(2000);
  25.     battle.engine();
  26.     if (battle.winner)
  27.     {
  28.         battle.draw();
  29.         FadeOut(2000);
  30.         battle.victory();
  31.     }
  32.     if (canlose)
  33.     {
  34.         return battle.winner;
  35.     }
  36.     if (!battle.winner && canlose == undefined)
  37.     {
  38.         battle.gameover();
  39.     }
  40.     RenderMap();
  41.     FadeIn(1000);
  42. }
  43.  
  44. function Battle(boss)
  45. {
  46.     this.boss_battle = boss;
  47.     this.finished = false;
  48.     this.winner;
  49.     this.situation = "Time";
  50.     this.enemy = new Array();
  51.     this.exp = 0;
  52.     this.money = 0;
  53.     this.ap = 0;
  54.     this.items = new Array();
  55.     this.current = undefined;
  56.     this.paused = false;
  57.     this.damage = new Array();
  58.     this.color = CreateColor(255, 255, 255);
  59. }
  60.  
  61. Battle.prototype.addEnemy = function(enemy, x, y)
  62. {
  63.     this.enemy[this.enemy.length] = enemy.clone();
  64.     this.enemy[this.enemy.length - 1].x = x;
  65.     this.enemy[this.enemy.length - 1].y = y;
  66. }
  67.  
  68. Battle.prototype.reset = function()
  69. {
  70.     this.finished = false;
  71.     this.winnder = undefined;
  72.     this.situation = "Time";
  73.     this.exp = 0;
  74.     this.money = 0;
  75.     this.ap = 0;
  76.     this.items = new Array();
  77.     this.current = undefined;
  78.     this.damage = new Array();
  79.     for (var i = 0; i < this.enemy.length; i ++)
  80.     {
  81.         this.enemy[i].hp = this.enemy[i].maxhp;
  82.         this.enemy[i].mp = this.enemy[i].maxmp;
  83.         this.enemy[i].ailments = AilmentArray();
  84.         this.enemy[i].situation = "Normal";
  85.         this.enemy[i].time = 0;
  86.     }
  87.     this.color.alpha = 255;
  88. }
  89.  
  90. Battle.prototype.engine = function()
  91. {
  92.     while (!this.finished)
  93.     {
  94.         this.draw();
  95.         this.update();
  96.         this.control();
  97.         FlipScreen();
  98.     }
  99. }
  100.  
  101. Battle.prototype.draw = function(focus)
  102. {
  103.     Interrupt();
  104.     this.background.blit(0, 0);
  105.     var yMinus = GetCharacters() * 12;
  106.     // Draw sprites
  107.     for (var i = 0; i < Game.psize; i ++)
  108.     {
  109.         Game.party[i].drawSprite();
  110.     }
  111.     for (var i = 0; i < this.enemy.length; i ++)
  112.     {
  113.         this.enemy[i].drawSprite();
  114.     }
  115.     // Draw effects
  116.     
  117.     // Draw damage
  118.     for (var i = 0; i < this.damage.length; i ++)
  119.     {
  120.         font.battleText(this.damage[i].target.x + this.damage[i].target.sprite.images[0].width / 2, this.damage[i].target.y + this.damage[i].target.sprite.images[0].height - 9 - (this.damage[i].time / 2), this.damage[i].amount + this.damage[i].type, "center", this.color);
  121.         if (this.damage[i].time < 42)
  122.         {
  123.             this.damage[i].time ++;
  124.         }
  125.         if (this.damage[0].time == 42 && i == this.damage.length - 1)
  126.         {
  127.             this.damage = new Array();
  128.             this.color.alpha = 255;
  129.         }
  130.     }
  131.     if (this.damage.length > 0 && this.damage[0].time > 10)
  132.     {
  133.         this.color.alpha -= 8;
  134.     }
  135.     // Draw character stuff
  136.     window.heading.drawWindow(144, 238 - yMinus, 170, 0);
  137.     font.littleText(148, 231 - yMinus, "NAME");
  138.     font.littleText(222, 231 - yMinus, "HP");
  139.     font.littleText(248, 231 - yMinus, "MP");
  140.     font.littleText(273, 231 - yMinus, "TIME");
  141.     if (this.current != undefined)
  142.     {
  143.         GradientRectangle(144, 238 - yMinus + this.current * 12, 174, 14, Colors.YellowS, Colors.YellowS2, Colors.YellowS2, Colors.YellowS);
  144.         Game.party[this.current].bMenu.draw(30, 180, 58, 4, focus);
  145.     }
  146.     for (var i = 0; i < Game.psize; i ++)
  147.     {
  148.         if (Game.party[i].available == true)
  149.         {
  150.             window.battleWindow(148, 240 - yMinus + i * 12, i);
  151.         }
  152.     }
  153.     if (this.paused == true)
  154.     {
  155.         ApplyColorMask(Colors.Shadow);
  156.         window.drawWindow(130, 100, 60, 5, true);
  157.         font.drawText(139, 98, "PAUSED");
  158.     }
  159. }
  160.  
  161. Battle.prototype.update = function()
  162. {
  163.     if (this.paused == false)
  164.     {
  165.         var victor = this.testWin();
  166.         if (victor == true)
  167.         {
  168.             this.finished = true;
  169.             this.winner = true;
  170.         }
  171.         if (victor == false)
  172.         {
  173.             this.finished = true;
  174.             this.winner = false;
  175.         }
  176.         if (this.situation == "Time")
  177.         {
  178.             // Update characters
  179.             for (var i = 0; i < Game.psize; i ++)
  180.             {
  181.                 if (IsAvailable(Game.party[i]))
  182.                 {
  183.                     Game.party[i].time += Game.party[i].speed;
  184.                     if (Game.party[i].time >= 1000)
  185.                     {
  186.                         Game.party[i].time = 1000;
  187.                         Game.party[i].turn = true;
  188.                         Game.party[i].curMenu = Game.party[i].bMenu;
  189.                         if (this.current == undefined)
  190.                         {
  191.                             this.current = i;
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.             // Update enemies
  197.         }
  198.     }
  199. }
  200.  
  201. Battle.prototype.control = function()
  202. {
  203.     if (LastPush())
  204.     {
  205.         if (IsKeyPressed(Game.start))
  206.         {
  207.             Game.last_push = GetTime();
  208.             if (this.paused == false)
  209.             {
  210.                 this.paused = true;
  211.             }
  212.             else
  213.             this.paused = false;
  214.         }
  215.         if (IsKeyPressed(Game.menu))
  216.         {
  217.             Game.last_push = GetTime();
  218.             if (this.current != undefined)
  219.             {
  220.                 this.nextCharacter();
  221.             }
  222.         }
  223.     }
  224.     if (this.current != undefined)
  225.     {
  226.         Game.party[this.current].bMenu.control();
  227.     }
  228. }
  229.  
  230. Battle.prototype.nextCharacter = function()
  231. {
  232.     Game.party[this.current].bMenu.reset();
  233.     var t = undefined;
  234.     for (var i = 0; t != undefined; i ++)
  235.     {
  236.         if (Game.party[this.current + 1 + i % Game.psize].turn == true)
  237.         {
  238.             t = i;
  239.         }
  240.     }
  241.     this.current = t;
  242. }
  243.  
  244. // Victory screen! :D
  245. Battle.prototype.victory = function()
  246. {
  247.     var expGiving = true;
  248.     var givenExp = 0;
  249.     var givenMoney = 0;
  250.     var moving = false;
  251.     var moneyGiving = true;
  252.     while (expGiving && moneyGiving)
  253.     {
  254.         Interrupt();
  255.         DrawBG();
  256.         window.tborder.drawWindow(16, 16, 150, 185);
  257.         for (var i = 0; i < 4; i ++)
  258.         {
  259.             window.victoryWindow(16, 16 + i * 47, Game.party[i]);
  260.         }
  261.         font.littleText(20, 8, "CURRENT PARTY");
  262.         window.drawWindow(183, 20, 117, 10, "Top", "INFO");
  263.         font.drawText(183, 20, "Received EXP and AP");
  264.         window.drawWindow(183, 51, 117, 44, true);
  265.         font.drawText(183, 51, "Experience Points");
  266.         font.drawText(300, 64, this.exp - givenExp, "right");
  267.         font.drawText(183, 76, "Ability Points (AP)");
  268.         font.drawText(300, 88, this.ap, "right");
  269.         window.drawWindow(183, 119, 117, 10, "Top", "INFO");
  270.         if (this.items.length > 0)
  271.         {
  272.             font.drawText(183, 119, "Found money & items");
  273.             window.drawWindow(183, 150, 117, 20 + (this.items.length * 16), true, "FUNDS & ITEM(S)");
  274.             for (var i = 0; i < this.items.length; i ++)
  275.             {
  276.                 font.iconText(183, 174 + i * 14, this.items[i]);
  277.                 font.drawText(300, 176 + i * 14, "x" + this.items[i].amount, "right");
  278.             }
  279.         }
  280.         else
  281.         {
  282.             font.drawText(183, 119, "Found money");
  283.             window.drawWindow(183, 150, 117, 20, true, "FUNDS");
  284.         }
  285.         font.drawText(300, 150, Game.money + "$", "right");
  286.         font.drawText(300, 162, this.money - givenMoney + "$", "right");
  287.         FlipScreen();
  288.         if (LastPush())
  289.         {
  290.             if (IsKeyPressed(Game.accept))
  291.             {
  292.                 if (!moving && givenExp < this.exp && givenMoney < this.money)
  293.                 {
  294.                     KeyPressed();
  295.                     moving = true;
  296.                 }
  297.                 else if (moving && givenExp < this.exp && givenMoney < this.money)
  298.                 {
  299.                     KeyPressed();
  300.                     moving = false;
  301.                     for (var i = 0; i < Game.psize; i ++)
  302.                     {
  303.                         Game.party[i].giveExp(this.exp - givenExp);
  304.                     }
  305.                     givenExp = this.exp;
  306.                     Game.money += this.money - givenMoney;
  307.                     givenMoney = this.money;
  308.                 }
  309.                 else if (!moving && this.exp == givenExp && this.money == givenMoney)
  310.                 {
  311.                     KeyPressed(false);
  312.                     expGiving = false;
  313.                     moneyGiving = false;
  314.                 }
  315.             }
  316.         }
  317.         if (moving && givenExp < this.exp)
  318.         {
  319.             for (var i = 0; i < Game.psize; i ++)
  320.             {
  321.                 Game.party[i].giveExp(1);
  322.             }
  323.             givenExp ++;
  324.         }
  325.         if (moving && givenMoney < this.money)
  326.         {
  327.             Game.money ++;
  328.             givenMoney ++;
  329.         }
  330.     }
  331. }
  332.  
  333. Battle.prototype.gameover = function()
  334. {
  335.     this.draw();
  336.     FadeOut(2000);
  337.     ApplyColorMask(Colors.Black);
  338.     Battles.gameover.blit(160 - Battles.gameover.width / 2, 120 - Battles.gameover.height / 2);
  339.     FadeIn(2000);
  340.     while (!IsKeyPressed(Game.accept))
  341.     {
  342.         Battles.gameover.blit(160 - Battles.gameover.width / 2, 120 - Battles.gameover.height / 2);
  343.         FlipScreen();
  344.     }
  345.     Battles.gameover.blit(160 - Battles.gameover.width / 2, 120 - Battles.gameover.height / 2);
  346.     FadeOut(2000);
  347.     Exit();
  348. }
  349.  
  350. Battle.prototype.testWin = function()
  351. {
  352.     var enDead = 0;
  353.     for (var i = 0; i < this.enemy.length; i ++)
  354.     {
  355.         if (this.enemy[i].hp == 0)
  356.         {
  357.             enDead ++;
  358.         }
  359.     }
  360.     if (enDead == this.enemy.length)
  361.     {
  362.         return true;
  363.     }
  364.     var plDead = 0;
  365.     for (var i = 0; i < Game.psize; i ++)
  366.     {
  367.         if (Game.party[i].hp == 0)
  368.         {
  369.             plDead ++;
  370.         }
  371.     }
  372.     if (plDead == Game.psize)
  373.     {
  374.         return false;
  375.     }
  376.     return undefined;
  377. }
  378.  
  379. function GiveDamage(target, amount, type)
  380. {
  381.     switch (type)
  382.     {
  383.         case "MP":
  384.             target.mp -= amount;
  385.             var damage = new Object();
  386.             damage.target = target;
  387.             damage.amount = amount;
  388.             damage.type = "MP";
  389.             damage.time = 0;
  390.             Game.curBattle.damage[Game.curBattle.damage.length] = damage;
  391.             if (target.mp < 0)
  392.             {
  393.                 target.mp = 0;
  394.             }
  395.             break;
  396.         default:
  397.             target.hp -= amount;
  398.             var damage = new Object();
  399.             damage.target = target;
  400.             damage.amount = amount;
  401.             damage.type = "";
  402.             damage.time = 0;
  403.             Game.curBattle.damage[Game.curBattle.damage.length] = damage;
  404.             if (target.hp < 0)
  405.             {
  406.                 target.hp = 0;
  407.                 target.time = 0;
  408.                 target.die();
  409.             }
  410.             break;
  411.     }
  412. }
  413.  
  414. // Ends turn
  415. function EndTurn(coe) // Character or enemy
  416. {
  417.     coe.turn = false;
  418.     coe.time = 0;
  419.     coe.situation = "Normal";
  420.     if (Game.party[Game.curBattle.current] == coe)
  421.     {
  422.         Game.curBattle.current = undefined;
  423.     }
  424.     // Check for damaging ailments
  425.     if (coe.ailments["Poison"] == true)
  426.     {
  427.         GiveDamage(coe, coe.hpmax * 0.05);
  428.     }
  429. }
  430.  
  431. function AvailableCharacters()
  432. {
  433.     var num = new Array();
  434.     for (var i = 0; i < Game.psize; i ++)
  435.     {
  436.         if (Game.party[i].hp > 0)
  437.         {
  438.             num[num.length] = i;
  439.         }
  440.     }
  441.     return num;
  442. }
  443.  
  444. function AvailableEnemies()
  445. {
  446.     var num = new Array();
  447.     for (var i = 0; i < Game.curBattle.enemy.length; i ++)
  448.     {
  449.         if (Game.curBattle.enemy[i].hp > 0)
  450.         {
  451.             num[num.length] = i;
  452.         }
  453.     }
  454.     return num;
  455. }
  456.  
  457. function IsAvailable(character)
  458. {
  459.     if (character.hp > 0 && character.available == true)
  460.     {
  461.         return true;
  462.     }
  463.     else
  464.         return false;
  465. }
  466.  
  467. function GetCharacters()
  468. {
  469.     var num = 0;
  470.     for (var i = 0; i < Game.psize; i ++)
  471.     {
  472.         if (Game.party[i].available == true)
  473.         {
  474.             num ++;
  475.         }
  476.     }
  477.     return num;
  478. }
  479.  
  480. var Battles = new Object();
  481. Battles.gameover = LoadImage("Game Over.png");
  482. // Music
  483. Battles.normal_music = LoadSound("Battle.it");
  484. // Backgrounds
  485. Battles.Plains = LoadImage("Plains-BG.png");
  486. // Battle objects
  487. Battles.Test1 = new Battle(false);
  488. //Battles.Test1.addEnemy(Enemies.Shitake, 56, 87);
  489. //Battles.Test1.addEnemy(Enemies.Jude, 23, 76);
  490. Battles.Test1.addEnemy(Enemies.Jude, 23, 104);